using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace DestructibleTextureDemo
{
    public enum Mode
    {
        Circle,
        Rectangle,
        Texture
    }

    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Mode mode;

        DestructibleTexture2D destructibleTexture;
        
        Texture2D texture;

        SpriteFont font;

        KeyboardState currentKeyboardState;
        KeyboardState previousKeyboardState;

        MouseState currentMouseState;
        MouseState previousMouseState;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = 700;
            graphics.PreferredBackBufferHeight = 400;

            Content.RootDirectory = "Content";

            mode = Mode.Circle;

            IsMouseVisible = true;

            currentKeyboardState = Keyboard.GetState();
            currentMouseState = Mouse.GetState();

            
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            

            // TODO: use this.Content to load your game content here

            //Load destructible texture
            Texture2D textureTemp = Content.Load<Texture2D>("Level");
            destructibleTexture  = new DestructibleTexture2D(textureTemp, GraphicsDevice);

            //Load other texture
            texture = Content.Load<Texture2D>("Star");

            //Load the font
            font = Content.Load<SpriteFont>("SpriteFont1");
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {   
            // TODO: Add your update logic here
            UpdateInput(gameTime);


            base.Update(gameTime);
        }

        protected void UpdateInput(GameTime gameTime)
        {
            previousKeyboardState = currentKeyboardState;
            currentKeyboardState = Keyboard.GetState();

            previousMouseState = currentMouseState;
            currentMouseState = Mouse.GetState();

            //Enter changes the mode
            if (currentKeyboardState.IsKeyDown(Keys.Enter) && previousKeyboardState.IsKeyUp(Keys.Enter))
                ToogleMode();

            //Space Resets the Texture
            if (currentKeyboardState.IsKeyDown(Keys.Space) && previousKeyboardState.IsKeyUp(Keys.Space))
                destructibleTexture.Reset();

            //Esc exists the game
            if (currentKeyboardState.IsKeyDown(Keys.Escape) && previousKeyboardState.IsKeyUp(Keys.Escape))
                Exit();

            //Mouse click destroys texture
            if (currentMouseState.LeftButton == ButtonState.Pressed && previousMouseState.LeftButton == ButtonState.Released)
                DestroyTexture(new Vector2(currentMouseState.X, currentMouseState.Y));
        }

        public void ToogleMode()
        {
            if (mode == Mode.Circle)
            {
                mode = Mode.Rectangle;
            }
            else if (mode == Mode.Rectangle)
            {
                mode = Mode.Texture;
            }
            else
            {
                mode = Mode.Circle;
            }
        }

        public void DestroyTexture(Vector2 position)
        {
            if(mode == Mode.Circle)
            {
                Circle circle = new Circle(position, 30);
                destructibleTexture.DestroyTexture(circle);
            }
            else if(mode == Mode.Rectangle)
            {

                int rectangleWidth = 70;
                int rectangleHeight = 40;

                //We want the center of the rectangle to be int the point of the click 
                float x = position.X - rectangleWidth/2f;
                float y = position.Y - rectangleHeight/2f;

                //Rectangle Settings
                Rectangle rectangle = new Rectangle((int) x,
                    (int) y,
                    rectangleWidth,
                    rectangleHeight);
                destructibleTexture.DestroyTexture(rectangle);
            }
            else if( mode == Mode.Texture)
            {
                //We want the texture to happear in the center of the click
                float x = position.X - texture.Width/2f;
                float y = position.Y - texture.Height/2f;

                destructibleTexture.DestroyTexture(new Vector2(x, y), texture, null);
            }

        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(new Color(0, 128, 224));

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            
            spriteBatch.Draw(destructibleTexture, Vector2.Zero, Color.White);

            //Draw the text

            spriteBatch.DrawString(font, "Mode:  ", new Vector2(20f, 20f), Color.Gold);
            spriteBatch.DrawString(font, mode.ToString(), new Vector2(100f, 20f), Color.White);
            spriteBatch.DrawString(font,  "Press Enter To Change Mode", new Vector2(20f, 40f), Color.Red);
            spriteBatch.DrawString(font, "Press Space To Reset", new Vector2(20f, 60f), Color.Red);
            spriteBatch.DrawString(font, "Press Escape To Exit", new Vector2(20f, 80f), Color.Red);

            spriteBatch.End();
            base.Draw(gameTime);

            //we have to force the graphics device before we change the texture-
            graphics.GraphicsDevice.Textures[0] = null;
        }
    }
}
